//
//  ParallaxVC.swift
//  elarc01
//
//  Created by elarc on 2016/9/26.
//  Copyright © 2016年 elarc. All rights reserved.
//

import Foundation

class ParallaxVC: UIViewController,UITableViewDelegate,UITableViewDataSource{
    
    let kNumberOfPerPage = 20
    let parallaxTableView = UITableView()
    var sourceArray:[AnyObject] = []
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "视差"
        self.sourceArray = []
        view.backgroundColor = UIColor.lightGrayColor()
        parallaxTableView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight - NavigationH)
        view.addSubview(parallaxTableView)
        parallaxTableView.delegate = self
        parallaxTableView.dataSource = self
        parallaxTableView.separatorStyle = UITableViewCellSeparatorStyle.None
        parallaxTableView.reloadData()
        
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return 20
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        handleSize()
    }

    func scrollViewDidScroll(scrollView: UIScrollView) {
        handleSize()
    }

    func handleSize() -> Void {
        parallaxTableView.visibleCells.forEach { (cell) in
            if let bannerCell = cell as? ParallaxCell{
                bannerCell.cellOnTableView(self.parallaxTableView, didScrollOnView: self.view)
            }
        }
    }
    let height_cell = floor(ScreenWidth*73/160)
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return  height_cell
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellId = "parallaxTableViewCell"
        var cell = tableView.dequeueReusableCellWithIdentifier(cellId) as? ParallaxCell
        if cell == nil {
            cell = ParallaxCell(style: UITableViewCellStyle.Value1, reuseIdentifier: cellId)
        }
        return cell!
    }
    
}

class ParallaxCell:UITableViewCell {
    
    let bgView = UIView()
    let bgImageView = UIImageView()
    let height_cell = floor(ScreenWidth*73/160)
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.whiteColor()
        selectionStyle = UITableViewCellSelectionStyle.None;
        contentView.backgroundColor = UIColor.whiteColor()
        configSubViews()
    }
    
    func configSubViews() -> Void {
        
        contentView.addSubview(bgView)
        
        bgView.frame = CGRectMake(0, 10, ScreenWidth ,ScreenHeight-10)
        
        bgView.clipsToBounds = true
        
        bgView.addSubview(bgImageView)
        bgImageView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight)
        bgImageView.image = UIImage(named: "07")
        bgImageView.userInteractionEnabled = true
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func cellOnTableView(tableView:UITableView,didScrollOnView:UIView) -> Void {
        let rectInSuperview:CGRect = tableView.convertRect(self.frame, toView:didScrollOnView)
        let distanceFromCenter:CGFloat = CGRectGetHeight(didScrollOnView.frame)/2 - CGRectGetMinY(rectInSuperview)
        let difference:CGFloat = CGRectGetHeight(self.bgImageView.frame) - CGRectGetHeight(self.frame)
        let move:CGFloat = distanceFromCenter / CGRectGetHeight(didScrollOnView.frame) * difference

        var imageRect:CGRect = (self.bgImageView.frame)
        imageRect.origin.y = -(difference/2)+move
        self.bgImageView.frame = imageRect
    }
    
}



